package org.rabbitmq.consumer;

import com.rabbitmq.client.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: zhs
 * @Date: 2021/6/5 14:36
 * @Description:
 *
 * WorkQueue(工作队列):
 *
 */

public class WorkQueueConsumer2 {

    public static void main(String[] args) throws IOException, TimeoutException {
        //1.创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        //2.设置参数
        factory.setHost("192.168.0.199"); //主机
        factory.setPort(5672); //端口
        factory.setVirtualHost("test"); //虚拟机
        factory.setUsername("root"); //用户名
        factory.setPassword("root"); //密码
        factory.setConnectionTimeout(5000);
        //3.获取链接 Connection
        Connection connection = factory.newConnection();
        //4.创建channel
        Channel channel = connection.createChannel();
        //5.创建队列
        channel.queueDeclare("work_queues",true,false,false,null);
        //接受消息
        Consumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.err.println(new String(body));
            }
        };

        channel.basicConsume("work_queues",true,consumer);
    }

}
